//https://www.nowcoder.com/practice/8ee967e43c2c4ec193b040ea7fbb10b8?tpId=13&tqId=23273&ru=%2Fpractice%2F59ac416b4b944300b617d4f7f111b215&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3FtpId%3D13
//思想1：flag 左移一位实现；
//思想2：n与n-1 进行比较,这种思想难度大，不容易想，但是效率极高；

#include <limits>
#include <vector>
#include <math.h>

using namespace std;

class Solution1 {
public:
    int NumberOf1(int n) {
        int count = 0;
        unsigned int flag = 1;
        while (flag) {
            if (n & flag) {
                ++count; 
            }
            flag = flag << 1;
        }
        return count;
    }
};


class Solution2 {
  public:
    int NumberOf1(int n) {
        int res = 0;
        //当n为0时停止比较
        while (n) {
            n = n&(n - 1);
            res++;
        }
        return res;
    }
};